package com.example.tool.service.dingtalk;

import com.dingtalk.api.response.OapiV2UserGetResponse;
import com.example.tool.bean.wcaiexcel.WechatCreateMemberRequest;
import com.example.tool.util.DDUtil;
import com.example.tool.util.WeChatUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class DingTalkBusinessServiceImpl implements DingTalkBusinessService {



    @Override
    public void addDingTalkUserSendToWeChat(String userId) {
        // 钉钉用户信息获取
        OapiV2UserGetResponse.UserGetResponse userGetResponse = DDUtil.queryUserInfo(userId);
        if (userGetResponse == null) {
            return;
        }
        WechatCreateMemberRequest memberRequest = new WechatCreateMemberRequest();
        memberRequest.setUserid(userGetResponse.getUserid());
        memberRequest.setName(userGetResponse.getName());
        memberRequest.setMobile(userGetResponse.getMobile());
        memberRequest.setPosition(userGetResponse.getTitle());
        memberRequest.setEmail(userGetResponse.getEmail());
        memberRequest.setEnable(1);
        boolean success = WeChatUtil.createMember(memberRequest);
        log.info("钉钉用户入职:{}同步到微信:{}", userGetResponse.getUserid(), success);
    }

    @Override
    public void deleteDingTalkUserSendToWeChat(String userId) {
        // 钉钉用户信息获取
        String mobile = DDUtil.queryLeaveUserInfo(userId);
        if (StringUtils.isBlank(mobile)) {
            log.error("钉钉用户手机号获取失败:{}", userId);
            return;
        }
        String wechatUserId = WeChatUtil.getUserIdByPhone(mobile);
        if (StringUtils.isBlank(wechatUserId)) {
            log.error("企微获取userid失败:{}", mobile);
            return;
        }
        boolean success = WeChatUtil.deleteMember(wechatUserId);
        log.info("钉钉用户离职:{}同步到微信:{}", userId, success);
    }

}
